package xyz.scootaloo.oj.iq

import org.junit.Test

/**
 * @author flutterdash@qq.com
 * @since 2021/4/15 9:03
 */
class M0103 {

    fun replaceSpaces(S: String, length: Int): String {
        var spaces = 0
        var count = length
        for (c in S) {
            if (count <= 0)
                break
            if (c == ' ')
                spaces++
            count--
        }

        val res = CharArray((length - spaces) + (spaces * 3))
        var index = 0
        count = length
        for (c in S) {
            if (count <= 0)
                break
            if (c == ' ') {
                res[index++] = '%'
                res[index++] = '2'
                res[index++] = '0'
            } else {
                res[index++] = c
            }
            count--
        }

        return String(res)
    }

    @Test
    fun test0() {
        println(replaceSpaces("Mr John Smith    ", 13))
        println(replaceSpaces("               ", 5))
    }

}
